<?php

//图片处理扩展类
class GraphicsExtend {

    static function ValidateCodeForCookie($var, $width = 43, $height = 18, $fontsize = 16) {
        setcookie($var, self::ValidateCodeMake($width, $height, $fontsize), null, '/');
    }

    static function ValidateCodeForSession($var) {
        session_start();
    }

    //创建验证码
    static function ValidateCodeMake($width = 43, $height = 18, $fontsize = 16) {
        $type = 'png';
        header("Content-type: image/" . $type);
        $tmp = rand(10000, 99999);
        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
            $im = imagecreatetruecolor($width, $height);
        } else {
            $im = imagecreate($width, $height);
        }
        $backColor = ImageColorAllocate($im, 0, rand(0, 127), 0); // 背景色（随机）
        $borderColor = ImageColorAllocate($im, 0, 0, 0); // 边框色
        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor); // 背景位置
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor); // 边框位置
        for ($i = 0; $i <= 100; $i++) {
            $pointX = rand(2, $width - 2);
            $pointY = rand(2, $height - 2);
            imagesetpixel($im, $pointX, $pointY, imagecolorallocatealpha($im, rand(0, 255), rand(0, 255), rand(0, 255),rand(0, 50)));
        }

        imagettftext($im, $fontsize, 0, 5, 20, imagecolorallocate($im, 127, 127, 127), root . '/font/arial.ttf', $tmp);

        $ImageFun = 'image' . $type;
        $ImageFun($im);
        ImageDestroy($im);
        return $tmp;
    }

    // 图片缩放：$path-原始图片路径,$mode-缩放模式(w:按宽,h:按高,z:按比例),$value:不同模式对应的值,$ext-文件保存地址中增加的字符
    public static function Zoom($path, $mode, $value, $ext = 'l') {
        $size = getimagesize($path);
        $s = $value; // 缩放比例
        switch ($mode) {
            case 'w':
                $s = $value / $size[0];
                break;
            case 'h':
                $s = $value / $size[1];
        }
        $w = round($size[0] * $s);
        $h = round($size[1] * $s);
        $im = imagecreatetruecolor($w, $h);
        switch ($size[2]) {
            case 2:
                $oim = imagecreatefromjpeg($path);
                break;
            case 3:
                $oim = imagecreatefrompng($path);
                break;
        }
        imagecopyresampled($im, $oim, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
        switch ($size[2]) {
            case 2:
                imagejpeg($im, str_replace('.jpg', $ext . '.jpg', $path));
                break;
            case 3:
                imagepng($im, str_replace('.png', $ext . '.png', $path));
                break;
        }
        imagedestroy($im);
    }

    public static function getImage($file) {
        $extname = pathinfo($file, PATHINFO_EXTENSION);
        if (!in_array($extname, ['jpg', 'jpeg', 'png', 'gif'])) {
            exit('0');
        }
        $img = call_user_func('imagecreatefrom' . ( $extname == 'jpg' ? 'jpeg' : $extname ), $file);
        return $img;
    }

    public static function gethashvalue($file, $w = 8, $h = 8) {
        $img = imagecreatetruecolor($w, $h);
        list($src_w, $src_h) = getimagesize($file);
        $src = GraphicsExtend::getImage($file);
        imagecopyresampled($img, $src, 0, 0, 0, 0, $w, $h, $src_w, $src_h);
        imagedestroy($src);

        $total = 0;
        $array = array();
        for ($y = 0; $y < $h; $y++) {
            for ($x = 0; $x < $w; $x++) {
                $gray = (imagecolorat($img, $x, $y) >> 8) & 0xFF;
                if (!isset($array[$y])) {
                    $array[$y] = array();
                }
                $array[$y][$x] = $gray;
                $total += $gray;
            }
        }

        imagedestroy($img);

        $average = intval($total / ($w * $h * 2));
        $hash = '';
        for ($y = 0; $y < $h; $y++) {
            for ($x = 0; $x < $w; $x++) {
                $hash .= ($array[$y][$x] >= $average) ? '1' : '0';
            }
        }
        var_dump($hash);
        return $hash;
    }

    public static function hashcompare($src1, $src2) {
        if (!is_file($src1) || !is_file($src2)) {
            exit('FILE_NOT_FOUND');
        }

        $hash1 = GraphicsExtend::getHashValue($src1);
        $hash2 = GraphicsExtend::getHashValue($src2);

        if (strlen($hash1) !== strlen($hash2)) {
            return false;
        }

        $count = 0;
        $len = strlen($hash1);
        for ($i = 0; $i < $len; $i++) {
            if ($hash1[$i] !== $hash2[$i]) {
                $count++;
            }
        }
        return $count <= 10 ? true : false;
    }

}

?>